package com.qf.boot220302.service.impl;


import cn.hutool.crypto.digest.DigestUtil;
import com.qf.boot220302.dao.UserDao;
import com.qf.boot220302.entity.User;
import com.qf.boot220302.ex.UserException;
import com.qf.boot220302.service.UserService;
import com.qf.boot220302.vo.TableVo;
import com.qf.boot220302.vo.UserRegistVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    UserDao userDao;
    @Value("${qfsy.fileupload.path}")
    String fileUploadPath;


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User login(String username, String password) {
        //1. 根据用户名查询用户信息
        User user =  userDao.getUserByName(username);

        if (user == null){  // 没用匹配的用户，直接返回null
            return null;
        }else {
            //2. 比对密码是否正确
            final String dbpassword = user.getPassword();

            // 转换 password 为 md5 字符串
            final String md5password = DigestUtil.md5Hex(password);

            if (dbpassword.equals(md5password)){
                return user;
            }

            return null;
        }
    }

    @Transactional(rollbackFor = Exception.class) //事务注解
    @Override
    public void regist(UserRegistVo uv) throws UserException, IOException {

        //1.用户名是否被占用
        final String username = uv.getUsername();
        final User dbUser = userDao.getUserByName(username);
        if (dbUser!=null){
            throw new UserException(-1,"用户名被占用");
        }


        //2.头像上传
        final MultipartFile photo = uv.getPhoto();

        // 生成一个唯一的文件名
        final String prefix = UUID.randomUUID().toString();
        final String fileName = prefix + "_" + photo.getOriginalFilename();

        final String finalFilepath = fileUploadPath + fileName;
        //存放文件
        photo.transferTo(new File(finalFilepath));
        log.debug("头像上传完毕");

        //存储数据库
        //2. 把用户的数据写入数据库表
        final User user = new User();

        user.setName(uv.getUsername());
        final String password = uv.getPassword();
        // password --->  md5
        final String md5Password = DigestUtil.md5Hex(password);
        user.setPassword(md5Password);

        user.setSex(uv.getSex());
        user.setBirthday(uv.getDate());

        user.setRegistTime(new Date());
        user.setPhotopath(fileName);

        userDao.insertUser(user);
        log.debug("用户数据落库成功,user={}",user);
    }
}
